package com.MSB2.sort2;

public class QuickSort {

    public static void main(String[] args) {
        int[] arr = {40, 3, 2, 45, 53, 2234, 12, 54, 5234, 32, 2347, 1, 343, 4};
        quickSort(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }

    public static void quickSort(int[] arr){
        if (arr == null || arr.length <= 1){
            return;
        }
        process(arr, 0, arr.length - 1);
    }
    public static void process(int[] arr, int left, int right){
        if (left >= right)
            return;
        int temp = arr[left];
        int l = left + 1;
        int r = right;
        while (l <= r){
            while (r >= l && arr[r] > temp){
                r --;
            }
            while (l <= r && arr[l] <= temp){
                l++;
            }

            if (l < r){
                int aaa = arr[l];
                arr[l] = arr[r];
                arr[r] = aaa;
            }
        }
        int aaa = arr[left];
        arr[left] = arr[r];
        arr[r] = aaa;

        process(arr, left, r-1);
        process(arr, l, right);
    }
}
